package com.firewolf.oa.base;

import java.lang.reflect.ParameterizedType;

import org.springframework.beans.factory.annotation.Autowired;

import com.firewolf.oa.service.DepartmentService;
import com.firewolf.oa.service.PermissionService;
import com.firewolf.oa.service.RoleService;
import com.firewolf.oa.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 基础Action， 提供getModel函数 注入所有的service
 * 
 * @author liuxing
 *
 * @param <T>
 */
public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T> {

	private static final long serialVersionUID = 8044559861585473524L;

	protected T model = null;
	/**
	 * 权限管理服务
	 */
	@Autowired
	protected PermissionService permissionService;
	
	/**
	 * 角色管理服务
	 */
	@Autowired
	protected RoleService roleService;

	/**
	 * 部门服务
	 */
	@Autowired
	protected DepartmentService deptService;

	/**
	 * 用户服务
	 */
	@Autowired
	protected UserService userService;

	@SuppressWarnings("unchecked")
	@Override
	public T getModel() {
		if (model == null) {
			try {
				ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
				Class<T> clazz = (Class<T>) pt.getActualTypeArguments()[0];
				model = clazz.newInstance();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return model;
	}

}
